package es.cazadoresmadridejos.beans.dao.biggameassistance.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import es.cazadoresmadridejos.beans.dao.biggameassistance.BigGameAssistanceDao;
import es.cazadoresmadridejos.common.bean.dao.impl.DaoImpl;
import es.cazadoresmadridejos.model.BigGameAssistance;

@SuppressWarnings("unchecked")
@Repository("bigGameAssistanceDao")
public class BigGameAssistanceDaoImpl extends DaoImpl<BigGameAssistance> implements BigGameAssistanceDao{

	@Override
	public BigGameAssistance newInstance() {
		return new BigGameAssistance();
	}

	@Override
	public List<BigGameAssistance> findAllByBigGame(final Long id) {
		return (List<BigGameAssistance>) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
					.append("select bga ")
					.append(" from BigGameAssistance bga inner join bga.bigGame bigGame")
					.append(" where bigGame.id = :bigGameId")
					.append("");

				Query query = session.createQuery(hql.toString());
				
				query.setLong("bigGameId", id);
				
				return query.list();
			}
		});
	}

}
